package com.qyl.http_upload.client;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/file")
public class FileController {

    @RequestMapping("/upload")
    public Map<String, Object> upload(
            @RequestParam("file")MultipartFile file, @RequestParam Map param){
        if(null != file && null != param){
            System.out.printf("上传文件名：【%s】，上传参数：【%s】",file.getOriginalFilename(), JSONObject.toJSONString(param));
        }
        if(null != file){
            System.out.printf("上传文件名：【%s】",file.getOriginalFilename());
        }
        return Map.of("msg","success");
    }
    @RequestMapping("/text")
    public Map<String, Object> text(
            @RequestParam(name="file",required = false)MultipartFile file, @RequestParam Map param) throws IOException {
        if(null != file){
            file.transferTo(new File("E:\\"+file.getOriginalFilename()));
            System.out.printf("上传文件名：【%s】",file.getOriginalFilename());
        }
        if(null != param){
            System.out.printf("上传参数：【%s】",JSONObject.toJSONString(param));
        }
        return Map.of("msg","success");
    }

    @GetMapping("/get")
    public Map<String, Object> get(@RequestParam("name") String name){
        System.out.println(name);
        return Map.of("msg","success");
    }

    class Vo{
        String id;
        String name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
